<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Create a new Query</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="class.mongodb-driver-query.html">« MongoDB\Driver\Query</a></li>
      <li style="float: right;"><a href="class.mongodb-driver-bulkwrite.html">MongoDB\Driver\BulkWrite »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.mongodb-driver-query.html">MongoDB\Driver\Query</a></li>
    <li>Create a new Query</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongodb-driver-query.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoDB\Driver\Query::__construct</h1>
  <p class="verinfo">(mongodb &gt;=1.0.0)</p><p class="refpurpose"><span class="refname">MongoDB\Driver\Query::__construct</span> &mdash; <span class="dc-title">Create a new Query</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongodb-driver-query.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">final</span> <span class="modifier">public</span> <span class="methodname"><strong>MongoDB\Driver\Query::__construct</strong></span>(<span class="methodparam"><span class="type"><span class="type">array</span>|<span class="type">object</span></span> <code class="parameter">$filter</code></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">array</span><span class="type"></span></span> <code class="parameter">$queryOptions</code><span class="initializer"> = <strong><code>null</code></strong></span></span>)</div>

  <p class="para rdfs-comment">
   Constructs a new <span class="classname"><a href="class.mongodb-driver-query.html" class="classname">MongoDB\Driver\Query</a></span>, which is an
   immutable value object that represents a database query. The query may then
   be executed with
   <span class="methodname"><a href="mongodb-driver-manager.executequery.html" class="methodname">MongoDB\Driver\Manager::executeQuery()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-mongodb-driver-query.construct-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
   
    <dt>
<code class="parameter">filter</code> (<span class="type"><span class="type">array</span>|<span class="type">object</span></span>)</dt>

    <dd>

     <p class="para">
      The <a href="https://www.mongodb.com/docs/manual/tutorial/query-documents/" class="link external">&raquo;&nbsp;query predicate</a>.
      An empty predicate will match all documents in the collection.
     </p>
     <blockquote class="note"><p><strong class="note">注意</strong>: 
      <span class="simpara">
       When evaluating query criteria, MongoDB compares types and values according to its own <a href="https://www.mongodb.com/docs/manual/reference/bson-type-comparison-order/" class="link external">&raquo;&nbsp;comparison rules for BSON types</a>, which differs from PHP&#039;s <a href="types.comparisons.html" class="link">comparison</a> and <a href="language.types.type-juggling.html" class="link">type juggling</a> rules. When matching a special BSON type the query criteria should use the respective <a href="book.bson.html" class="link">BSON class</a> (e.g. use <span class="classname"><a href="class.mongodb-bson-objectid.html" class="classname">MongoDB\BSON\ObjectId</a></span> to match an <a href="https://www.mongodb.com/docs/manual/reference/bson-types/#objectid" class="link external">&raquo;&nbsp;ObjectId</a>).
      </span>
     </p></blockquote>
    </dd>

   

   
    <dt>
<code class="parameter">queryOptions</code></dt>

    <dd>

     <p class="para">
      <table id="mongodb-driver-query.construct-queryOptions" class="doctable table">
       <caption><strong>queryOptions</strong></caption>
       
        <thead>
         <tr>
          <th>Option</th>
          <th>Type</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td>allowDiskUse</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            Allows MongoDB to use temporary disk files to store data exceeding
            the 100 megabyte system memory limit while processing a blocking
            sort operation.
           </p>
          </td>
         </tr>

         <tr>
          <td>allowPartialResults</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            For queries against a sharded collection, returns partial results
            from the mongos if some shards are unavailable instead of throwing
            an error.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;partial&quot;</code> option if
            not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>awaitData</td>
          <td><span class="type">bool</span></td>
          <td>
           Use in conjunction with the <code class="literal">&quot;tailable&quot;</code> option to
           block a getMore operation on the cursor temporarily if at the end of
           data rather than returning no data. After a timeout period, the query
           returns as normal.
          </td>
         </tr>

         <tr>
          <td>batchSize</td>
          <td><span class="type">int</span></td>
          <td>
           <p class="para">
            The number of documents to return in the first batch. Defaults to
            101. A batch size of 0 means that the cursor will be established,
            but no documents will be returned in the first batch.
           </p>
           <p class="para">
            In versions of MongoDB before 3.2, where queries use the legacy wire
            protocol OP_QUERY, a batch size of 1 will close the cursor
            irrespective of the number of matched documents.
           </p>
          </td>
         </tr>

         
         <tr>
          <td>collation</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            <a href="https://www.mongodb.com/docs/upcoming/reference/collation/" class="link external">&raquo;&nbsp;Collation</a> allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the <code class="literal">&quot;locale&quot;</code> field is mandatory; all other collation fields are optional. For descriptions of the fields, see <a href="https://www.mongodb.com/docs/upcoming/reference/collation/#collation-document" class="link external">&raquo;&nbsp;Collation Document</a>.
           </p>
           <p class="para">
            If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.
           </p>
           <p class="para">
            This option is available in MongoDB 3.4+ and will result in an exception at execution time if specified for an older server version.
           </p>
          </td>
         </tr>


         <tr>
          <td>comment</td>
          <td><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></td>
          <td>
           <p class="para">
            An arbitrary comment to help trace the operation through the
            database profiler, currentOp output, and logs.
           </p>
           <p class="para">
            The comment can be any valid BSON type for MongoDB 4.4+. Earlier
            server versions only support string values.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$comment&quot;</code> modifier
            if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>exhaust</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            Stream the data down full blast in multiple &quot;more&quot; packages, on the
            assumption that the client will fully read all data queried. Faster
            when you are pulling a lot of data and know you want to pull it all
            down. Note: the client is not allowed to not read all the data
            unless it closes the connection.
           </p>
           <p class="para">
            This option is not supported by the find command in MongoDB 3.2+ and
            will force the driver to use the legacy wire protocol version (i.e.
            OP_QUERY).
           </p>
          </td>
         </tr>

         <tr>
          <td>explain</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            If <strong><code>true</code></strong>, the returned <span class="classname"><a href="class.mongodb-driver-cursor.html" class="classname">MongoDB\Driver\Cursor</a></span>
            will contain a single document that describes the process and
            indexes used to return the query.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$explain&quot;</code> modifier
            if not specified.
           </p>
           <p class="para">
            This option is not supported by the find command in MongoDB 3.2+ and
            will only be respected when using the legacy wire protocol version
            (i.e. OP_QUERY). The
            <a href="https://www.mongodb.com/docs/manual/reference/command/explain/" class="link external">&raquo;&nbsp;explain</a>
            command should be used on MongoDB 3.0+.
           </p>
          </td>
         </tr>

         <tr>
          <td>hint</td>
          <td><span class="type"><span class="type">string</span>|<span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            Index specification. Specify either the index name as a string or
            the index key pattern. If specified, then the query system will only
            consider plans using the hinted index.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;hint&quot;</code> option if not
            specified.
           </p>
          </td>
         </tr>

         
         <tr>
          <td>let</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g. <code class="literal">$$var</code>).
           </p>
           <p class="para">
            This option is available in MongoDB 5.0+ and will result in an exception at execution time if specified for an older server version.
           </p>
          </td>
         </tr>


         <tr>
          <td>limit</td>
          <td><span class="type">int</span></td>
          <td>
           <p class="para">
            The maximum number of documents to return. If unspecified, then
            defaults to no limit. A limit of 0 is equivalent to setting no
            limit.
           </p>
           <p class="para">
            A negative limit is will be interpreted as a positive limit with the
            <code class="literal">&quot;singleBatch&quot;</code> option set to <strong><code>true</code></strong>. This behavior
            is supported for backwards compatibility, but should be considered
            deprecated.
           </p>
          </td>
         </tr>

         <tr>
          <td>max</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            The <em class="emphasis">exclusive</em> upper bound for a specific index.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$max&quot;</code> modifier if
            not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>maxAwaitTimeMS</td>
          <td><span class="type">int</span></td>
          <td>
           <p class="para">
            Positive integer denoting the time limit in milliseconds for the
            server to block a getMore operation if no data is available. This
            option should only be used in conjunction with the
            <code class="literal">&quot;tailable&quot;</code> and <code class="literal">&quot;awaitData&quot;</code>
            options.
           </p>
          </td>
         </tr>

         <tr>
          <td>maxScan</td>
          <td><span class="type">int</span></td>
          <td>
           <div class="warning"><strong class="warning">警告</strong>
            <p class="simpara">
             This option is deprecated and should not be used.
            </p>
           </div>
           <p class="para">
            Positive integer denoting the maximum number of documents or index
            keys to scan when executing the query.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$maxScan&quot;</code> modifier
            if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>maxTimeMS</td>
          <td><span class="type">int</span></td>
          <td>
           <p class="para">
            The cumulative time limit in milliseconds for processing operations
            on the cursor. MongoDB aborts the operation at the earliest
            following interrupt point.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$maxTimeMS&quot;</code>
            modifier if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>min</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            The <em class="emphasis">inclusive</em> lower bound for a specific index.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$min&quot;</code> modifier if
            not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>modifiers</td>
          <td><span class="type">array</span></td>
          <td>
           <a href="https://www.mongodb.com/docs/manual/reference/operator/query-modifier/" class="link external">&raquo;&nbsp;Meta operators</a>
           modifying the output or behavior of a query. Use of these operators
           is deprecated in favor of named options.
          </td>
         </tr>

         <tr>
          <td>noCursorTimeout</td>
          <td><span class="type">bool</span></td>
          <td>
           Prevents the server from timing out idle cursors after an inactivity
           period (10 minutes).
          </td>
         </tr>

         <tr>
          <td>oplogReplay</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            Internal use for replica sets. To use oplogReplay, you must include
            the following condition in the filter:
           </p>
           <p class="para">
            <div class="example-contents">
<div class="textcode"><pre class="textcode">[ &#039;ts&#039; =&gt; [ &#039;$gte&#039; =&gt; &lt;timestamp&gt; ] ]</pre>
</div>
            </div>

           </p>
           <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">This option is deprecated as of the 1.8.0 release.</span></p></blockquote>
          </td>
         </tr>

         <tr>
          <td>projection</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">
            The <a href="https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/" class="link external">&raquo;&nbsp;projection specification</a>
            to determine which fields to include in the returned documents.
           </p>
           <p class="para">
            If you are using the <a href="mongodb.persistence.deserialization.html" class="link">ODM
            functionality</a> to deserialise documents as their original
            PHP class, make sure that you include the
            <span class="property">__pclass</span> field in the projection. This is
            required for the deserialization to work and without it, the
            driver will return (by default) a <span class="classname"><a href="class.stdclass.html" class="classname">stdClass</a></span>
            object instead.
           </p>
          </td>
         </tr>

         <tr>
          <td>readConcern</td>
          <td><span class="classname"><a href="class.mongodb-driver-readconcern.html" class="classname">MongoDB\Driver\ReadConcern</a></span></td>
          <td>
           <p class="para">
            A read concern to apply to the operation. By default, the read
            concern from the
            <a href="mongodb-driver-manager.construct.html#mongodb-driver-manager.construct-uri" class="link">MongoDB
            Connection URI</a> will be used.
           </p>
           <p class="para">
            This option is available in MongoDB 3.2+ and will result in an
            exception at execution time if specified for an older server
            version.
           </p>
          </td>
         </tr>

         <tr>
          <td>returnKey</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            If <strong><code>true</code></strong>, returns only the index keys in the resulting documents.
            Default value is <strong><code>false</code></strong>. If <strong><code>true</code></strong> and the find command does not
            use an index, the returned documents will be empty.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$returnKey&quot;</code>
            modifier if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>showRecordId</td>
          <td><span class="type">bool</span></td>
          <td>
           <p class="para">
            Determines whether to return the record identifier for each
            document. If <strong><code>true</code></strong>, adds a top-level <code class="literal">&quot;$recordId&quot;</code>
            field to the returned documents.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$showDiskLoc&quot;</code>
            modifier if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>singleBatch</td>
          <td><span class="type">bool</span></td>
          <td>
           Determines whether to close the cursor after the first batch.
           Defaults to <strong><code>false</code></strong>.
          </td>
         </tr>

         <tr>
          <td>skip</td>
          <td><span class="type">int</span></td>
          <td>Number of documents to skip. Defaults to 0.</td>
         </tr>

         <tr>
          <td>snapshot</td>
          <td><span class="type">bool</span></td>
          <td>
           <div class="warning"><strong class="warning">警告</strong>
            <p class="simpara">
             This option is deprecated and should not be used.
            </p>
           </div>
           <p class="para">
            Prevents the cursor from returning a document more than once because
            of an intervening write operation.
           </p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$snapshot&quot;</code> modifier
            if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>sort</td>
          <td><span class="type"><span class="type">array</span>|<span class="type">object</span></span></td>
          <td>
           <p class="para">The sort specification for the ordering of the results.</p>
           <p class="para">
            Falls back to the deprecated <code class="literal">&quot;$orderby&quot;</code> modifier
            if not specified.
           </p>
          </td>
         </tr>

         <tr>
          <td>tailable</td>
          <td><span class="type">bool</span></td>
          <td>Returns a tailable cursor for a capped collection.</td>
         </tr>

        </tbody>
       
      </table>

     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 errors" id="refsect1-mongodb-driver-query.construct-errors">
  <h3 class="title">错误／异常</h3>
  <ul class="simplelist">
   <li class="member">Throws <span class="classname"><a href="class.mongodb-driver-exception-invalidargumentexception.html" class="classname">MongoDB\Driver\Exception\InvalidArgumentException</a></span> on argument parsing errors.</li>
  </ul>
 </div>


 <div class="refsect1 changelog" id="refsect1-mongodb-driver-query.construct-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>PECL mongodb 1.14.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;let&quot;</code> option. The
         <code class="literal">&quot;comment&quot;</code> option now accepts any type.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.8.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;allowDiskUse&quot;</code> option.
        </p>
        <p class="para">
         The <code class="literal">&quot;oplogReplay&quot;</code> option is deprecated.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.5.0</td>
       <td>
        <p class="para">
         The <code class="literal">&quot;maxScan&quot;</code> and <code class="literal">&quot;snapshot&quot;</code>
         options are deprecated.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.3.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;maxAwaitTimeMS&quot;</code> option.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.2.0</td>
       <td>
        <p class="para">
         Added the <code class="literal">&quot;allowPartialResults&quot;</code>,
         <code class="literal">&quot;collation&quot;</code>, <code class="literal">&quot;comment&quot;</code>,
         <code class="literal">&quot;hint&quot;</code>, <code class="literal">&quot;max&quot;</code>,
         <code class="literal">&quot;maxScan&quot;</code>, <code class="literal">&quot;maxTimeMS&quot;</code>,
         <code class="literal">&quot;min&quot;</code>, <code class="literal">&quot;returnKey&quot;</code>,
         <code class="literal">&quot;showRecordId&quot;</code>, and <code class="literal">&quot;snapshot&quot;</code>
         options.
        </p>
        <p class="para">
         Renamed the <code class="literal">&quot;partial&quot;</code> option to
         <code class="literal">&quot;allowPartialResults&quot;</code>. For backwards compatibility,
         <code class="literal">&quot;partial&quot;</code> will still be read if
         <code class="literal">&quot;allowPartialResults&quot;</code> is not specified.
        </p>
        <p class="para">
         Removed the legacy <code class="literal">&quot;secondaryOk&quot;</code> option, which is
         obsolete. For queries using the legacy wire protocol OP_QUERY, the
         driver will set the <code class="literal">secondaryOk</code> bit as needed in
         accordance with the
         <a href="https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst" class="link external">&raquo;&nbsp;Server Selection Specification</a>.
        </p>
       </td>
      </tr>

      <tr>
       <td>PECL mongodb 1.1.0</td>
       <td>Added the <code class="literal">&quot;readConcern&quot;</code> option.</td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongodb-driver-query.construct-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1309">
   <p><strong>示例 #1 <span class="function"><strong>MongoDB\Driver\Query::__construct()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Select&nbsp;only&nbsp;documents&nbsp;authord&nbsp;by&nbsp;"bjori"&nbsp;with&nbsp;at&nbsp;least&nbsp;100&nbsp;views&nbsp;*/<br /></span><span style="color: #0000BB">$filter&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'author'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bjori'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'views'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$gte'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;],<br />];<br /><br /></span><span style="color: #0000BB">$options&nbsp;</span><span style="color: #007700">=&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Only&nbsp;return&nbsp;the&nbsp;following&nbsp;fields&nbsp;in&nbsp;the&nbsp;matching&nbsp;documents&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'projection'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'title'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'article'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;],<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Return&nbsp;the&nbsp;documents&nbsp;in&nbsp;descending&nbsp;order&nbsp;of&nbsp;views&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'sort'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;[<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'views'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;-</span><span style="color: #0000BB">1<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">],<br />];<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Query</span><span style="color: #007700">(</span><span style="color: #0000BB">$filter</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$manager&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">Manager</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost:27017'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$readPreference&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">ReadPreference</span><span style="color: #007700">(</span><span style="color: #0000BB">MongoDB</span><span style="color: #007700">\</span><span style="color: #0000BB">Driver</span><span style="color: #007700">\</span><span style="color: #0000BB">ReadPreference</span><span style="color: #007700">::</span><span style="color: #0000BB">RP_PRIMARY</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$manager</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">executeQuery</span><span style="color: #007700">(</span><span style="color: #DD0000">'databaseName.collectionName'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$readPreference</span><span style="color: #007700">);<br /><br />foreach(</span><span style="color: #0000BB">$cursor&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$document</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$document</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mongodb-driver-query.construct-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="methodname"><a href="mongodb-driver-manager.executequery.html" class="methodname" rel="rdfs-seeAlso">MongoDB\Driver\Manager::executeQuery()</a> - Execute a database query</span></li>
   <li class="member"><span class="classname"><a href="class.mongodb-driver-cursor.html" class="classname">MongoDB\Driver\Cursor</a></span></li>
  </ul>
 </div>


</div></div></div></body></html>